package main.java;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

public class CookieAwareWebClient {
	CookieManager cookie = new CookieManager();
	private List<String> cookies;
	private Hashtable<String, String> headers = new Hashtable<String, String>();

	public CookieAwareWebClient() {
		this.cookie.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
		CookieHandler.setDefault(this.cookie);
	}

	private void addHeaders(URLConnection paramURLConnection) {
		Enumeration<String> localEnumeration = this.headers.keys();
		while (true) {
			if (!localEnumeration.hasMoreElements())
				return;
			String str = (String) localEnumeration.nextElement();
			paramURLConnection.addRequestProperty(str,
					(String) this.headers.get(str));
		}
	}

	public InputStream UploadData(String paramString1, String paramString2,
			String paramString3) throws IOException {
		System.out.println(paramString1+" - "+paramString2);
		URLConnection localURLConnection = new URL(paramString1)
				.openConnection();
		addHeaders(localURLConnection);
		((HttpURLConnection) localURLConnection).setRequestMethod(paramString2);
		localURLConnection.setDoOutput(true);
		DataOutputStream localDataOutputStream = new DataOutputStream(
				localURLConnection.getOutputStream());
		localDataOutputStream.write(paramString3.getBytes("US-ASCII"));
		localDataOutputStream.flush();
		return new BufferedInputStream(localURLConnection.getInputStream());
	}

	public void addHeader(String paramString1, String paramString2) {
		if (paramString2 != null)
			this.headers.put(paramString1, paramString2);
	}

	public void dropPhishingCookies() {
		if (this.cookies != null)
			;
		for (int i = 0;; i++) {
			if (i >= this.cookies.size())
				return;
			if (((String) this.cookies.get(i)).startsWith("FUTWebPhishing")) {
				this.cookies.remove(i);
				i--;
			}
		}
	}

	public String getSecurityCookie() {
		if (this.cookies != null)
			;
		for (int i = 0;; i++) {
			if (i >= this.cookies.size())
				;
			for (String str = null;; str = (String) this.cookies.get(i)) {
				if (!((String) this.cookies.get(i))
						.startsWith("FUTWebPhishing"))
					break;
				return str;
			}
		}
	}

	public InputStream openRead(String paramString)
			throws MalformedURLException, IOException {
		URLConnection localURLConnection = new URL(paramString)
				.openConnection();
		addHeaders(localURLConnection);
		localURLConnection.setDoInput(true);
		localURLConnection.connect();
		return new BufferedInputStream(localURLConnection.getInputStream());
	}

	public void removeHeader(String paramString) {
		this.headers.remove(paramString);
	}

	public void setSecurityCookie(String paramString) {
		if (this.cookies == null)
			this.cookies = new ArrayList<String>();
		this.cookies.add(paramString);
	}
}